import java.util.Arrays;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: LianBao
 * Date: 2024-02-06
 * Time: 15:35
 */
public class Test {
    //练习


    public static void reverse(int[] arr) {
        int left = 0;
        int right = arr.length - 1;
        while (left < right) {
            int tmp = arr[left];
            arr[left] = arr[right];
            arr[right] = tmp;
            left++;
            right--;
        }
    }

    public static double Average(int[] arr) {

        double sum = 0;
        for (int j : arr) {
            sum += j;
        }
        return sum / arr.length;
    }

    public static int find(int[] arr, int x) {
        //数组中查找找x返回下标
        for (int i = 0; i < arr.length; i++) {
            if (arr[i] == x) {
                return i;
            }
        }
        return -1;
    }

    public static int binarySearch(int[] arr, int x) {
        //二分查找
        int left = 0;
        int right = arr.length - 1;
        while (left <= right) {
            int mid = (left + right) / 2;
            if (arr[mid] < x) {
                left = mid + 1;
            } else if (arr[mid] > x) {
                right = mid - 1;
            } else {
                return mid;
            }
        }
        return -1;
    }


    public static void main(String[] args) {
        int[] arr = {1, 2, 3, 4, 5};
//        double avg = Average(arr);
////        System.out.println(avg);
////        System.out.println(find(arr, 100));
//        System.out.println(binarySearch(arr, 3));
//        //sort排序
//        int[] arr2 = {1, 3, 2, 7, 6, 3, 11, 30};
//        Arrays.sort(arr2);
////        Arrays.binarySearch(arr,3);自带的二分查找
        int[] arr3 = {1, 2, 3, 4, 5};
        reverse(arr3);
        System.out.println(Arrays.toString(arr3));

    }


    //>>数组拷贝
//    Arrays.copyOf用的多
    public static int[] copy(int[] tmp) {

        int[] newArray = new int[tmp.length];
        for (int i = 0; i < tmp.length; i++) {
            newArray[i] = tmp[i];
        }
        return newArray;
    }

    public static void main3(String[] args) {
        //拷贝array数组
        int[] array = {1, 3, 5, 7, 9};
        int[] newArray = copy(array);
        System.out.println(Arrays.toString(newArray));
        //直接使用方法拷贝Arrays.copyOf(原数组,长度)
        int[] newArray2 = Arrays.copyOf(array, array.length);
        //扩容:Arrays.copyOf(array,2*array.length);

    }

    public static void main5(String[] args) {
        int[] array = {1, 2, 3, 4};
//        int[] array2 = System.arraycopy();可以局部拷贝
//        System.arraycopy();参数:1.要拷贝的数组(array1),2.拷贝的数组(array1)的开始位置3.目的数组(array2)4.目的地数组(array2)的开始位置5.拷贝的长度
        int[] arr = Arrays.copyOfRange(array, 1, 3);//拷贝(1下标到3下标,左闭右开,结果是2,3)//也可以局部
        System.out.println(Arrays.toString(arr));
//        int[] arr2 = arr.clone();产生一个副本
    }

    //>>数组转字符串
    public static String myToString(int[] tmp) {
        //自己实现toString
//        assert tmp!= null;//assert不生效,可能需要前提
        if (tmp == null) {
            return "null";
        }
        String str = "[";
        for (int i = 0; i < tmp.length; i++) {
            str += tmp[i];
            if (i != tmp.length - 1) {
                str = str + ", ";
            }
        }
        str += "]";
        //[1, 2, 3, 4]
        return str;
    }

    public static void main4(String[] args) {
        int[] array = {1, 2, 3, 4};
//        String ret = Arrays.toString(array);
        String ret = myToString(array);
        System.out.println(ret);
    }


    public static void main2(String[] args) {
        int[] arr = {1, 2, 3, 4};//整体初始化,只有一次机会!就是在定义的同时进行初始化
        /*
        int[] arr2;
        arr2 = {1, 2, 3};
        错误的,只能再new一个
        */

    }

    public static void fun(int[] tmp) {
//        tmp = {1, 2, 3, 4};
        tmp = new int[]{1, 2, 3, 4};
    }

    public static void func1(int[] arr) {
        //数组 都是对象
        for (int j : arr) {
            System.out.printf("%d ", j);
        }
    }

    public static void func2(int[] array) {
        array = new int[5];
    }

    public static void main1(String[] args) {
        int[] array = {1, 2, 3, 4, 5};
        func1(array);
        int[] array2 = null;//表示array2这个引用没有任何对象(不引用对象)
        int[] array3 = new int[]{1, 2, 3, 4};

    }

    public static void print(int n) {
        if (n < 10) {
            System.out.println(n);
            return;
        }
        print(n / 10);
        System.out.println(n % 10);
    }
}
